var DAO=require('./DAO')
//针对用户数据表操作的模块对象
var menuDAO = {
    //TOP榜单
    getAllTop: function (callback) {
        DAO('select * from mm join (menu,meal) on mm.tomenuId = menu.menuId and mm.tomealId = meal.mealId order by meallikeNum desc', null, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //榜单详情
    getTopDetail: function (mealId,callback) {
        DAO('select menuId,menuName,menuPic,goods,menuValue,menumake from menu,mm where menu.menuId=mm.tomenuId and tomealId = ?',[mealId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //文章标题图片
    getPeople: function (callback) {
        DAO('select * from people', null, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //文章具体内容
    getPeopleDetail: function (peopleId, callback) {
        DAO('select healthTitle,healthWord,healthPic from people where peopleId = ?', peopleId, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //查询套餐点赞记录表用户点赞记录
    mealLike:function (user, callback) {
        DAO('select tomealId from meallike where touserId = (select userId from users where phone = ?)',[user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //插入套餐点赞记录
    insertLike: function (meal, callback) {
        DAO('insert into meallike(tomealId,touserId) values(?,(select userId from users where phone = ?))',[meal.mealId,meal.user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //增加套餐点赞数
    addLikeNum: function (mealId, callback) {
        DAO('update meal set meallikeNum=meallikeNum+1 where mealId = ?',mealId, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    //删除套餐点赞记录
    deleteLike: function (meal, callback) {
        DAO('delete from meallike where tomealId = ? and touserId = (select userId from users where phone = ?)',[meal.mealId,meal.user.phone], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //取消套餐点赞
    delLikeNum: function (mealId, callback) {
        DAO('update meal set meallikeNum=meallikeNum-1 where mealId = ?', mealId, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    }
}
module.exports=menuDAO